<?php

class JgdWebUser extends CWebUser {

    protected $_model;

    function isSuperAdmin(){
        $user = $this->loadUser();
        if($user)
            return $user->member_type == LevelLookUp::SUPERADMIN;
        return false;
    }
    
    function isAdmin() {
        $user = $this->loadUser();
        if ($user)
            return $user->member_type == LevelLookUp::ADMIN;
        return false;
    }

    protected function loadUser() {
        if ($this->_model === null) {
            $this->_model = Member::model()->findByPk($this->id);
        }
        return $this->_model;
    }
    
    function havePackage() {
        $user = $this->loadUser();
        if($this->getRemainingPackageDay($user->member_id)<=0){
            $user->package_id = null;
            $user->save();
        }
        $package = Package::model()->findByAttributes(array('package_id' => $user->package_id));
        if($package == NULL){
            return false;
        }        
        return true;
    }
    
    function totalWithdrawRequest() {
        $criteria = new CDbCriteria();
        $criteria->addCondition('status=0');
        $withdraw = Withdraw::model()->findAll($criteria);
        return sizeof($withdraw);
    }
    
    function getRemainingPackageDay($id){
        $sql = "select * from member_transaction_history where member_id =" . $id . " and isDeposit='1' order by member_transaction_history_id desc limit 1";

        $_lh = Yii::app()->db->createCommand($sql)->queryAll();
        $tdate = 0;
        if (!empty($_lh)) {
            $_history = $_lh[0];
            if (!empty($_history)) {
                $tdate = $_history['transaction_date'];
            }
        }
        
        // get current date and time
        $cdate = date('Y-m-d H:i:s');
        
        $startTimeStamp = strtotime($tdate);
        $endTimeStamp = strtotime($cdate);

        $timeDiff = abs($endTimeStamp - $startTimeStamp);
        $numberDays = $timeDiff / 86400;  // 86400 seconds in one day
        $numberDays = intval($numberDays);
        $remainingday = 180 - $numberDays;
        
        return $remainingday; 
    }
}

?>
